/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.order.rest;

import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BizException;
import me.zhengjie.modules.order.domain.SoldOrder;
import me.zhengjie.modules.order.domain.req.ApplySoldReq;
import me.zhengjie.modules.order.domain.req.OrderAuditReq;
import me.zhengjie.modules.order.domain.resp.OrderListResp;
import me.zhengjie.modules.order.domain.resp.SoldOrderDetailResp;
import me.zhengjie.modules.order.domain.resp.SoldOrderListResp;
import me.zhengjie.modules.order.service.SoldOrderService;
import me.zhengjie.modules.order.domain.vo.SoldOrderQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author yiwei.he
* @date 2024-04-25
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "售后订单管理")
@RequestMapping("/api/soldOrder")
@Slf4j
public class SoldOrderController {

    private final SoldOrderService soldOrderService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check()")
    public void exportSoldOrder(HttpServletResponse response, SoldOrderQueryCriteria criteria) throws IOException {
        soldOrderService.download(soldOrderService.queryAll(criteria), response);
    }

    @GetMapping("/admin/list")
    @Log("查询售后订单")
    @ApiOperation("查询售后订单")
    public ResponseEntity<PageResult<SoldOrderListResp>> querySoldOrder(SoldOrderQueryCriteria criteria, Page<Object> page){
        log.info("/api/soldOrder/admin/list 售后订单列表（后管）：{},分页参数：{}",criteria,page);
        return new ResponseEntity<>(soldOrderService.list(criteria,page),HttpStatus.OK);
    }

    @PostMapping("/list")
    @Log("查询售后订单(小程序)")
    @ApiOperation("查询售后订单(小程序)")
    public ResponseEntity<PageResult<SoldOrderListResp>> list(@RequestBody SoldOrderQueryCriteria criteria){
        log.info("/api/soldOrder/list 售后订单列表（小程序）：{}",criteria);
        return new ResponseEntity<>(soldOrderService.list(criteria,criteria.getPage()),HttpStatus.OK);
    }

    @GetMapping("/detail/{orderSn}")
    @Log("售后订单详情(小程序)")
    @ApiOperation("售后订单详情(小程序)")
    public ResponseEntity<SoldOrderDetailResp> detail(@PathVariable String orderSn){
        log.info("/api/soldOrder/detail/{} 售后订单详情（小程序）",orderSn);
        return new ResponseEntity<>(soldOrderService.detail(orderSn),HttpStatus.OK);
    }

    @PostMapping("/apply")
    @Log("申请售后")
    @ApiOperation("申请售后")
    public ResponseEntity<Object> apply(@Validated @RequestBody ApplySoldReq req) throws BizException {
        log.info("/api/soldOrder/apply 售后订单详情（小程序）:{}",req);
        return new ResponseEntity<>(soldOrderService.apply(req),HttpStatus.OK);
    }

    @PostMapping("/audit")
    @Log("订单状态[1.待审核，2.已审核待收货，3.已完成，4.已拒绝]")
    @ApiOperation("订单状态[1.待审核，2.已审核待收货，3.已完成，4.已拒绝]")
    public ResponseEntity<Object> checkStatus(@Validated @RequestBody OrderAuditReq req) throws BizException {
        log.info("/api/soldOrder/audit 售后订单详情（小程序）:{}",req);
        return new ResponseEntity<>(soldOrderService.audit(req),HttpStatus.OK);
    }


}